# Appendix I
# Santiago Lopez-Cariboni, 2022. "Political Regimes and Informal Social Insurance", Comparative Political Studies.

# ---- wd ----
wd <- 'replication_materials' 
setwd(wd)

# ---- packages ----
pkgs <- c("data.table", "foreign", 'panelView', 'fect', "lubridate", 
    "ggplot2", "dplyr", "mFilter", "texreg", "AER", "xts", "interflex", "gridExtra")
suppressPackageStartupMessages(sapply(pkgs,require,character.only=T))
# Plot the effects (use Johannes Karreth's function: https://github.com/jkarreth/JKmisc/blob/master/ggintfun.R)
# devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/ggintfun.R")
source('code/functions/ggintfun.R')
# Function for time Lags
source('code/functions/lagpanel.R')


# ---- data ----
dt <- fread('data/dt_replication.csv')
dt <- dt[LDC==1, ]

# ---- Table 5 ----
dt <- dt[order(dt$iso3c, dt$year),] # sort or lagpanel
dt$residential_price <- dt$`Residential_Total price (2015 USD/MWh using PPP)`/100
dt$l.residential_price <- lagpanel(dt$`Residential_Total price (2015 USD/MWh using PPP)`/100, dt$iso3c, dt$year, 1) 

m <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.residential_price
    - l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[,], na.action=na.omit)
summary(m,  diagnostics = TRUE)
nc <- length(unique(m$model[['as.factor(iso3c)']]))

m.price.demo <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton
    + l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.price.demo,  diagnostics = TRUE)
nc.price.demo <- length(unique(m.price.demo$model[['as.factor(iso3c)']]))

m.price.auto <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton
    + l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
summary(m.price.auto,  diagnostics = TRUE)
nc.price.auto <- length(unique(m.price.auto$model[['as.factor(iso3c)']]))

m.price <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.price,  diagnostics = TRUE)
nc.price <- length(unique(m.price$model[['as.factor(iso3c)']]))


m.price.controls <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.residential_price
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.price.controls,  diagnostics = TRUE)
nc.price.controls <- length(unique(m.price.controls$model[['as.factor(iso3c)']]))

v.names <- c(
    "Losses Cycle$_{t-1}$",
    "GDP Output Gap$_{t-1}$",
    "Democracy$_{t-1}$",
    "Democracy$_{t-1}$ $\\times$ GDP Output Gap$_{t-1}$",
    "Residential electricity price (USD/MWh using PPP)$_{t-1}$",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Electricity Consumption$_{t-1}$",
    "Population Density$_{t-1}$"
    )


table1 <- texreg(list(m, m.price.demo,  m.price.auto, m.price, m.price.controls),
    file= "tables/OA/T5_price.tex",
    label="tab:prices",
    custom.model.names = c('(1)', '(Democracies)','(Autocracies)', '(4)', '(5)'),
    caption="Ciclicality of Electricity Losses in Autocracies and Democracies controlling for Electricity Prices in the Residential Sector",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes", "Yes", "Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes", "Yes", "Yes", "Yes"),
        "Number of countries"= c(nc, nc.price.demo, nc.price.auto, nc.price, nc.price.controls)),
    # reorder.gof = c(1, 2, 5, 3, 4),
    custom.coef.names=v.names
    )




# ---- Table 6 ----
dt$l.ele_production_oil   <- lagpanel(dt$ele_production_oil, dt$iso3c, dt$year, 1 )
dt$l.ele_production_hydro  <- lagpanel(dt$ele_production_hydro, dt$iso3c, dt$year, 1)
dt$l.ele_production_nuclear  <- lagpanel(dt$ele_production_nuclear, dt$iso3c, dt$year, 1)
dt$l.crude_price_barrel  <- lagpanel(dt$crude_price_barrel, dt$iso3c, dt$year, 1)
dt$l.oilrents_gdp  <- lagpanel(dt$oilrents_gdp, dt$iso3c, dt$year, 1)

m <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.ele_production_oil 
    + l.ele_production_hydro
    + l.ele_production_nuclear
    + l.oilrents_gdp
    + l.crude_price_barrel
    - l.ele_production_oil 
    - l.ele_production_hydro
    - l.ele_production_nuclear
    - l.oilrents_gdp
    - l.crude_price_barrel
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[,], na.action=na.omit)
summary(m,  diagnostics = TRUE)
nc <- length(unique(m$model[['as.factor(iso3c)']]))

m.price <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.ele_production_oil 
    + l.ele_production_hydro
    + l.ele_production_nuclear
    + l.oilrents_gdp
    + l.crude_price_barrel
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.price,  diagnostics = TRUE)
nc.price <- length(unique(m.price$model[['as.factor(iso3c)']]))


m.price.controls <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.democracy
    + l.ele_production_oil 
    + l.ele_production_hydro
    + l.ele_production_nuclear
    + l.oilrents_gdp
    + l.crude_price_barrel
    + l.capacity
    # + l.elec.access
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    # + gdpconstus_new
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.price.controls,  diagnostics = TRUE)
nc.price.controls <- length(unique(m.price.controls$model[['as.factor(iso3c)']]))

v.names <- c(
    "Losses Cycle$_{t-1}$",
    "GDP Output Gap$_{t-1}$",
    "Democracy$_{t-1}$",
    "Democracy$_{t-1}$ $\\times$ GDP Output Gap$_{t-1}$",
    "Electricity production from oil sources (\\% of total)$_{t-1}$",
    "Electricity production from hydroelectric sources (\\% of total)$_{t-1}$",
    "Electricity production from nuclear sources (\\% of total)$_{t-1}$",
    "Oil rents (\\% GDP)$_{t-1}$",
    "International oil prices (USD per barrel)$_{t-1}$",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Electricity Consumption$_{t-1}$",
    "Population Density$_{t-1}$"
    )



table1 <- texreg(list(m, m.price, m.price.controls),
    file= "tables/OA/T6_price_factors.tex",
    label="tab:prices_factors",
    custom.model.names = c('(1)', '(2)', '(3)'),
    caption="Ciclicality of Electricity Losses in Autocracies and Democracies controlling for Factors Affectind Domestic Electricity Prices",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes", "Yes"),
        "Number of countries"= c(nc, nc.price, nc.price.controls)),
    # reorder.gof = c(1, 2, 5, 3, 4),
    custom.coef.names=v.names
    )




# ---- Table 7 ----

m <- lm(residential_price ~ 
    + l.democracy
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[,], na.action=na.omit)
summary(m,  diagnostics = TRUE)
nc <- length(unique(m$model[['as.factor(iso3c)']]))


m.c <- lm(residential_price ~ 
    + l.democracy
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[,], na.action=na.omit)
summary(m.c,  diagnostics = TRUE)
nc.c <- length(unique(m.c$model[['as.factor(iso3c)']]))


m.dem <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.dem,  diagnostics = TRUE)
nc.dem <- length(unique(m.dem$model[['as.factor(iso3c)']]))



m.dem.c <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.residential_price
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.dem.c,  diagnostics = TRUE)
nc.dem.c <- length(unique(m.dem.c$model[['as.factor(iso3c)']]))


m.auto <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.residential_price
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
summary(m.auto,  diagnostics = TRUE)
nc.auto <- length(unique(m.auto$model[['as.factor(iso3c)']]))


m.auto.c <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * l.residential_price
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
summary(m.auto.c,  diagnostics = TRUE)
nc.auto.c <- length(unique(m.auto.c$model[['as.factor(iso3c)']]))


v.names <- c(
    "Democracy$_{t-1}$",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Electricity Consumption$_{t-1}$",
    "Population Density$_{t-1}$",
    "Losses Cycle$_{t-1}$",
    "GDP Output Gap$_{t-1}$",
    "Electricity price$_{t-1}$",
    "Electricity price$_{t-1}$ $\\times$ GDP Output Gap$_{t-1}$"
    )



table1 <- texreg(list(m,m.c,m.dem,m.dem.c,m.auto,m.auto.c),
    file= "tables/OA/T7_prices_democracy.tex",
    label="tab:prices_democracy",
    custom.header = list("DV:prices" = 1:2, "DV:Losses" = 3:6), 
    custom.model.names = c('(1)', '(2)','(Democracies)', '(Democracies)', '(Autocracies)','(Autocracies)'),
    caption="Effect of Democracy on Residential Prices, and Ciclicality of Electricity Losses Conditional on Residential Prices",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
        "Number of countries"= c(nc,nc.c,nc.dem,nc.dem.c,nc.auto,nc.auto.c))
    ,
    # reorder.gof = c(11, 9, 1, 10, 12, 2, 3, 4, 5, 6, 7, 8),
    custom.coef.names=v.names
    )
















